// api/order.js
const { get, post } = require("../utils/request.js");
const { getCustomerAddress: getAddressList } = require("./address.js");

/**
 * 获取用户收货地址列表
 */
const getCustomerAddress = () => {
  return getAddressList();
};

/**
 * 创建订单
 * @param {Object} params - 参数
 * @param {Number} params.orderType - 订单类型：1-普通，2-满减，3-折扣，4-特价
 * @param {String} params.addressId - 用户收货地址ID
 * @param {String} params.remark - 下单备注
 * @param {Boolean} params.usePoints - 是否使用积分
 * @param {Boolean} params.create - false：预览订单，true：确认下单
 * @param {Array} params.items - 商品列表
 */
const createOrder = (params) => {
  return post("/client/order/createOrder", params);
};

/**
 * 获取微信支付参数
 * @param {Object} params - 参数
 * @param {String} params.orderId - 订单ID
 */
const getPaymentParams = (params) => {
  return post("/client/pay/jsapi/prepay", params);
};

/**
 * 获取订单状态统计
 * @param {Number|null} orderType - 订单类型：null-全部，1-普通，2-满减，3-折扣，4-特价
 * @returns {Promise} 返回各状态订单数量统计
 */
const getOrderStatusCount = (orderType = null) => {
  const url = orderType
    ? `/client/order/orderStatusCount?orderType=${orderType}`
    : `/client/order/orderStatusCount?orderType=`;
  return get(url);
};

/**
 * 获取订单详情
 * @param {String} orderId - 订单ID
 * @returns {Promise} 返回订单详情信息
 */
const getOrderDetail = (orderId) => {
  return get(`/client/order/detail/${orderId}`);
};

module.exports = {
  getCustomerAddress,
  createOrder,
  getPaymentParams,
  getOrderStatusCount,
  getOrderDetail,
};
